package com.whfc.fuum.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.fuum.dao.SysExternalLinkMapper;
import com.whfc.fuum.dao.SysSkinMapper;
import com.whfc.fuum.dto.SysExternalLinkDTO;
import com.whfc.fuum.dto.SysSkinDTO;
import com.whfc.fuum.entity.SysSkin;
import com.whfc.fuum.manager.link.SysLinkMgr;
import com.whfc.fuum.manager.link.SysLinkMgrFactory;
import com.whfc.fuum.param.SysSkinAdd;
import com.whfc.fuum.param.SysSkinEdit;
import com.whfc.fuum.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-12-15 17:40
 */
@DubboService(interfaceClass = SysConfigService.class, version = "1.0.0")
public class SysConfigServiceImpl implements SysConfigService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 默认ID
     */
    private static final Integer DEFAULT_DEPT_ID = 0;

    @Autowired
    private SysSkinMapper sysSkinMapper;

    @Autowired
    private SysExternalLinkMapper sysExternalLinkMapper;

    @Autowired
    private SysLinkMgrFactory sysLinkMgrFactory;

    @Override
    public SysSkinDTO getSkin(String host) throws BizException {
        logger.info("查询域名配置,host:{}", host);
        SysSkinDTO sysSkin = sysSkinMapper.selectByHost(host);
        boolean isEmptyLogo = StringUtils.isBlank(sysSkin.getLogo());
        boolean isEmptyTitleLogo = StringUtils.isBlank(sysSkin.getTitleLogo());
        if (isEmptyLogo || isEmptyTitleLogo) {
            SysSkinDTO defaultSkin = sysSkinMapper.selectByHost("");
            if (isEmptyLogo) {
                sysSkin.setLogo(defaultSkin.getLogo());
            }
            if (isEmptyTitleLogo) {
                sysSkin.setTitleLogo(defaultSkin.getTitleLogo());
            }
        }
        return sysSkin;
    }

    @Override
    public PageData<SysSkinDTO> getSkinList(Integer pageNum, Integer pageSize) throws BizException {
        PageHelper.startPage(pageNum, pageSize);
        List<SysSkinDTO> list = sysSkinMapper.selectAll();
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public List<SysSkinDTO> getSkinList() throws BizException {
        return sysSkinMapper.selectAll();
    }

    @Override
    public void addSkin(SysSkinAdd param) throws BizException {
        SysSkin skin = new SysSkin();
        BeanUtils.copyProperties(param, skin);
        sysSkinMapper.insertSelective(skin);
    }

    @Override
    public void editSkin(SysSkinEdit param) throws BizException {
        SysSkin skin = new SysSkin();
        BeanUtils.copyProperties(param, skin);
        skin.setId(param.getSkinId());
        sysSkinMapper.updateByPrimaryKeySelective(skin);
    }

    @Override
    public void delSkin(Integer skinId) throws BizException {
        sysSkinMapper.logicDeleteById(skinId);
    }

    @Override
    public SysExternalLinkDTO getLinkUrl(Integer deptId, String code, Integer userId) throws BizException {
        SysExternalLinkDTO sysExternalLink = sysExternalLinkMapper.selectExternalLinkDTO(deptId, code);
        if (sysExternalLink == null) {
            sysExternalLink = sysExternalLinkMapper.selectExternalLinkDTO(DEFAULT_DEPT_ID, code);
        }
        if (sysExternalLink != null && StringUtils.isNotBlank(sysExternalLink.getUrl())) {
            SysLinkMgr sysLinkMgr = sysLinkMgrFactory.getSysLinkMgr(code);
            String url = sysLinkMgr.getLinkUrl(deptId, userId, sysExternalLink.getUrl());
            sysExternalLink.setUrl(url);
        }
        logger.info("获取系统外链,deptId:{},userId:{},code:{},url:{}", deptId, userId, code, sysExternalLink);
        return sysExternalLink;
    }

    @Override
    public SysSkinDTO getSkinByDeptId(Integer deptId) {
        return sysSkinMapper.getSkinByDeptId(deptId);
    }
}
